<?php

namespace App\Http\Admin\Services\Alimama;

use App\Exceptions\InvalidRequestException;
use App\Http\Models\AlimamaOrder;
use App\Lib\PHPExcel\ExcelHandle;
use Illuminate\Support\Facades\DB;


class OrderService
{
    private $data;



    public function import(){
        $arr = ExcelHandle::read1();

        $this->dataOperatio($arr);
    }

    public function dataOperatio($arr){
        if(empty($arr)){
            throw new InvalidRequestException('表格无数据');
        }

        foreach ($arr as $item){
            $tmp = [];
            $obj = $this->_getItem($item['O']);

            $tmp['order_id'] = $item['O'];
            $tmp['order_status'] = $item['P'];
            $tmp['pay_time'] = $item['C'];
            $tmp['shop_name'] = $item['J'];
            $tmp['pay_amount'] = $item['R'];
            $tmp['title'] = $item['H'];
            $tmp['income_rate'] = (float)$item['Y']/100;
            $tmp['fc_rate'] = (float)$item['Z']/100;

            $tmp['platform_tax'] = (float)$tmp['pay_time'] *(float) $tmp['income_rate'] * (float)(1-(float)$tmp['fc_rate']);
            $tmp['commission'] = $item['AD'];
            $tmp['tech_fee'] = $item['AC'];
            $tmp['media_name'] = $item['AI'];
            $tmp['promotion'] = $item['AK'];

            $quality =  $obj ? $this->_getOrderQuality($obj,$tmp['order_status']) : 0;
            if($quality){
                $tmp['quality'] = $quality;
            }

            if(!$obj){
                AlimamaOrder::create($tmp);
            }else{
                AlimamaOrder::where('order_id',$tmp['order_id'])->update($tmp);
            }
        }

    }
    public function _getItem($order_id){
        return AlimamaOrder::where('order_id',$order_id)->first();
    }



    public function _getOrderQuality($obj,$order_status){

        if($order_status != '已失效' || $obj['order_status'] == '已失效'){
            return 0;
        }

        $startdate = $obj['pay_time'];
        $enddate = date('Y-m-d H:i:s',time());
        $date=ceil((strtotime($enddate)-strtotime($startdate))/86400);

        return (int)$date;
    }


    public function list($conditions = []){
        $query = $this->getQuery($conditions);

        if(!empty($conditions['order_key'])){
            $query->orderBy($conditions['order_key'],$conditions['order_value']);
        }
        $result = $query->paginate(50);

        foreach ($result as $item){
            $item['actualScm'] = $this->getActualScm($conditions,$item['scm_name']);
            $item['cancelScm'] = $this->getCancelScm($conditions,$item['scm_name']);
            $item['moreThenFive'] = $this->moreThenFive($conditions,$item['scm_name']);
            $item['lessThenFive'] = $this->lessThenFive($conditions,$item['scm_name']);
        }

        return $result;
    }

    public function getCancelScm($conditions,$scm_name){
        $query = $this->getQuery($conditions)->where('order_status','已失效');
        return $query->get()->where('scm_name',$scm_name)->first();
    }

    public function getActualScm($conditions,$scm_name){
        $query = $this->getQuery($conditions)->where('order_status','!=','已失效');

        return $query->get()->where('scm_name',$scm_name)->first();
    }

    public function MoreThenFive($conditions,$scm_name){
        $query = $this->getQuery($conditions)->where('o.quality','>',5);
        return $query->get()->where('scm_name',$scm_name)->first();
    }

    public function lessThenFive($conditions,$scm_name){
        $query = $this->getQuery($conditions)->where('o.quality','<',5)->where('o.quality','>',0);
        return $query->get()->where('scm_name',$scm_name)->first();
    }


    public function getQuery($conditions = []){
        $query = AlimamaOrder::query();
        $raw = DB::raw("
           s.scm_name,
           count(*) as total_order_sum,
           sum(o.pay_amount) as total_pay_amount,
           sum(o.commission) as total_commission,
           sum(o.tech_fee) as total_tech_fee,
           sum(o.platform_tax) as total_platform_tax,
           (sum(o.commission) - sum(o.tech_fee)) as final_benifit
           
        ");
        $query = $query->from('admin_alimama_order as o')
            ->leftJoin('admin_scm_has_shops as s', 's.shop_name', '=', 'o.shop_name')
            ->select($raw);


        $query = $query->where('o.media_name','西瓜直播');
        if(!empty($conditions['start_time'])){
            $query->where('o.pay_time','>',$conditions['start_time']);
        }
        if(!empty($conditions['end_time'])){
            $query->where('o.pay_time','<',$conditions['end_time']);
        }

        return $query->groupBy('s.scm_name');
    }


    public function orderList($conditions = []){
        $query = AlimamaOrder::query();
        $raw = DB::raw("
           s.scm_name,
           o.*
        ");
        $query = $query->from('admin_alimama_order as o')
            ->leftJoin('admin_scm_has_shops as s', 's.shop_name', '=', 'o.shop_name')
            ->select($raw);


        $query = $query->where('o.media_name','西瓜直播');
        if(!empty($conditions['start_time'])){
            $query->where('o.pay_time','>',$conditions['start_time']);
        }
        if(!empty($conditions['end_time'])){
            $query->where('o.pay_time','<',$conditions['end_time']);
        }


        return $query->orderBy('o.pay_time','desc')->paginate(50);
    }








}
